package com.greenlife.controller.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.greenlife.commom.ResponseException;
import com.greenlife.common.enums.BaseResultCodeEnum;
import com.greenlife.model.FileServerConfig;

@Component
public class UrlUtil {
	
	@Autowired
	private FileServerConfig fileServerConfig1;
	private static FileServerConfig fileServerConfig;
	
	@PostConstruct
	public void Init(){
		fileServerConfig = this.fileServerConfig1;
	}
	
	/**
	 * 将json格式的url反序列化为List<String>
	 * @param source
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<String> urlDeserialize(String source){
		System.err.println(source);
		try {
			List<String> list = new ArrayList<String>();
			if(source==null || "".equals(source)) return list;
			list = JsonUtil.DB_EXTENSION_INSTANCE.readValue(source,list.getClass());			
			return list;
		} catch (IOException e) {
			throw new ResponseException(BaseResultCodeEnum.ERR_CODE_00001);
		}		
	}
	

	/**
	 * 添加新的url
	 * @param oldUrls
	 * @param newUrls
	 * @return
	 */
	public static List<String> addNewUrls(List<String> oldUrls,List<String> newUrls){
		if(oldUrls.size()==fileServerConfig.getMaxNumber()||oldUrls.size()+newUrls.size()>fileServerConfig.getMaxNumber()){
			throw new ResponseException(BaseResultCodeEnum.ERR_CODE_00001);
		}
		for (int i = 0; i < newUrls.size(); i++) {
			oldUrls.add(newUrls.get(i));
		}		
		return oldUrls;
	}
	
	
	
	/**
	 * 序列化
	 * @param urlList
	 * @return 
	 */
	public static String urlSerialize(List<String> urlList){
		try {
			return JsonUtil.DB_EXTENSION_INSTANCE.writeValueAsString(urlList);
		} catch (JsonProcessingException e) {
			throw new ResponseException(BaseResultCodeEnum.ERR_CODE_00001);	
		}
	}
	
	
	
	/**
	 * 文件url拼接域名
	 * @param urlList
	 * @return
	 */
	public static List<String> urlAddDomain(List<String> urlList){
		if(urlList==null) return null;
		for (int i = 0; i < urlList.size(); i++) {
			  urlList.set(i,fileServerConfig.getDomain()+urlList.get(i));
		}
		return urlList;
	}
	
	/**
	 * 文件url拼接域名
	 * @param url
	 * @return
	 */
	public static String urlAddDomain(String url){
		return fileServerConfig.getDomain()+url;
	}
	
	/**
	 * 剪切url的域名
	 * @param urlList
	 * @return
	 */
	public static List<String> urlCutDomain(List<String> urlList){
		if(urlList==null) return null;
		for (int i = 0; i < urlList.size(); i++) {
			  String url = urlList.get(i);
			  if(url!=null) url = url.replace(fileServerConfig.getDomain(), "");
			  urlList.set(i,url);
		}
		return urlList;
	}
	
	
	/**
	 * 剪切url的域名
	 * @param urlList
	 * @return
	 */
	public static String urlCutDomain(String url){
		if(url!=null) 
			url = url.replace(fileServerConfig.getDomain(), "");	
		return url;
	}
	
	
	public static void main(String[] args) throws JsonProcessingException {
		List<String> list = new ArrayList<String>();
		String nString = "22222\"1111111\"2222222";
		nString = nString.replace("\\","");
		list.add(nString);
		list.add("111111\"2222\"1111111");
		System.err.println(JsonUtil.DB_EXTENSION_INSTANCE.writeValueAsString(list));
	}
	
}
